package com.pengtu.web.project.org;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.project.DesignOrganization;
import com.pengtu.service.project.SignOrganizationManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 单位管理
 * 
 * @author libitum
 * 
 */
@Namespace("/project/org")
@Results({ @Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "organization.action"),
	@Result(name = BaseActionSupport.VIEW, location = "/project/sign/org/organization!signlist.action?typeId=${typeId}", type = "redirect"),
	@Result(name = "orgsuc", location = "/orgsuccess.jsp", type = "redirect")
})

public class DesignorgAction extends BaseActionSupport<DesignOrganization> {

	private static final long serialVersionUID = 1L;

	private SignOrganizationManager signOrgManager;

	// -- 页面属性 --//
	private String id;
	private DesignOrganization designOrg;
	private Page<DesignOrganization> page = new Page<DesignOrganization>(10);// 每页5条记录
	private String typeId;

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	@Autowired
	public void setSignOrgManager(SignOrganizationManager signOrgManager) {
		this.signOrgManager = signOrgManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Page<DesignOrganization> getPage() {
		return page;
	}

	public void setPage(Page<DesignOrganization> page) {
		this.page = page;
	}

	@Override
	public DesignOrganization getModel() {
		return designOrg;
	}
	
	@Action("/quality/org/designorg")
	public String list() throws Exception {
		QueryFilter queryfilters = QueryFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = signOrgManager.findPageDesignOrg(page,queryfilters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		System.out.println(typeId);
		signOrgManager.saveDesignOrg(designOrg);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"单位成功");
		if(StringUtils.isNotEmpty(typeId)){
			Struts2Utils.getSession().setAttribute("orgId",  "");
			Struts2Utils.getSession().setAttribute("orgName",  "");
			Struts2Utils.getSession().setAttribute("orgId",  designOrg.getId());
			System.out.println(Struts2Utils.getSession().getAttribute("orgId"));
			Struts2Utils.getSession().setAttribute("orgName",  designOrg.getOrgName());
			System.out.println(Struts2Utils.getSession().getAttribute("orgName"));
			return "orgsuc";
		}
		return input();
	}

	/**
	 * 验证机构代码是否重复
	 */
	public void checkOrgNum() {
		// TODO Auto-generated method stub
		String orgNum = Struts2Utils.getParameter("orgNum");
		String oldOrgNum = Struts2Utils.getParameter("oldOrgNum");
		if (signOrgManager.isOrgNumUnique(orgNum, oldOrgNum)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	/**
	 * 验证单位名是否重复
	 */
	public void checkOrgName() {
		// TODO Auto-generated method stub
		String orgName = Struts2Utils.getParameter("orgName");
		String oldOrgName = Struts2Utils.getParameter("oldOrgName");
		if (signOrgManager.isOrgNameUnique(orgName, oldOrgName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		addActionMessage("删除设计单位" + designOrg.getOrgName());
		signOrgManager.deleteDesignOrg(id);
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			designOrg = signOrgManager.getDesign("id",id);
		} else {
			designOrg = new DesignOrganization();
		}
	}

}
